package com.example.database.config;

import com.zaxxer.hikari.HikariDataSource;
import com.zaxxer.hikari.pool.ProxyConnection;
import lombok.Getter;
import org.postgresql.shaded.com.ongres.scram.common.util.Preconditions;
import org.springframework.context.annotation.Primary;
import org.springframework.jdbc.datasource.ConnectionHolder;
import org.springframework.stereotype.Component;

import javax.sql.DataSource;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.SQLException;

/**
 * @author zpMeng
 * @since 10/9/2025
 */
public class EvictableDataSource extends HikariDataSource {

    private final HikariDataSource delegate;

    public EvictableDataSource(DataSource dataSource) {
        this.delegate = dataSource instanceof HikariDataSource hikariDataSource
                ? hikariDataSource
                : null;
        Preconditions.checkNotNull(delegate, "only support hikari datasource");
    }

    @Override
    public Connection getConnection() throws SQLException {
        return new CusConnection(delegate.getConnection(), this);
    }

}
